<?php

//运费模版
// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Store\Controller;

use Store\Controller\StoreController;

class TransportController extends StoreController {

    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'goods'); // 数据集
    }

    public function index() {

        $map['store_id'] = session('store_id');
        $list = M('transport')->where($map)->select();
        foreach ($list as $key => $value) {
            $where['transport_id'] = $value['id'];
            $list[$key]['trans'] = M('transport_extend')->where($where)->select();
        }
        $this->assign('list', $list);
        $this->assign('action', 'transport');
        $this->display();
    }

    public function add() {
        if (IS_POST) {
            $areass = explode('|||', I('areas')['kd'][1]);
            $city = M('area')->field('area_id,area_name')->where('area_parent_id=0')->select();
            $province = array();
            $tmp = explode(',', $areass[0]);

            if (!empty($tmp) && is_array($tmp)) {
                foreach ($tmp as $t) {
                    $pid = $city[$t]['area_id'];
                    if (!in_array($pid, $province) && !empty($pid))
                        $province[] = $pid;
                }
            }

            if (count($province) > 0) {
                $a = implode(',', $province);
                $data['top_area_id'] = ',' . $a . ',';
            } else {
                $data['top_area_id'] = '';
            }

            $data['title'] = I('title');
            $data['send_tpl_id'] = 1;
            $data['store_id'] = session('store_id');
            $data['update_time'] = NOW_TIME;
            $data['num'] = I('num');
            $data['money'] = I('money');
            $rs = M('transport')->add($data);
            if ($rs) {
                $default = I('default');
                $datas['snum'] = $default['kd']['start'];
                $datas['sprice'] = $default['kd']['postage'];
                $datas['xnum'] = $default['kd']['plus'];
                $datas['xprice'] = $default['kd']['postageplus'];
                $datas['is_default'] = 1;
                $datas['transport_id'] = $rs;
                $datas['transport_title'] = I('title');
                $datas['area_name'] = '全国';
                M('transportExtend')->add($datas);
                $areas = I('areas')['kd'];
                $special = I('special')['kd'];
                if (is_array($areas) && is_array($special)) {
                    foreach ($special as $key => $value) {
                        if (empty($areas[$key]))
                            continue;
                        $areas[$key] = explode('|||', $areas[$key]);
                        $dataa[$key + 1]['area_id'] = ',' . $areas[$key][0] . ',';
                        $dataa[$key + 1]['area_name'] = $areas[$key][1];
                        $dataa[$key + 1]['snum'] = $value['start'];
                        $dataa[$key + 1]['sprice'] = $value['postage'];
                        $dataa[$key + 1]['xnum'] = $value['plus'];
                        $dataa[$key + 1]['xprice'] = $value['postageplus'];
                        $dataa[$key + 1]['is_default'] = 2;
                        $dataa[$key + 1]['transport_id'] = $rs;
                        $dataa[$key + 1]['transport_title'] = I('title');

                        //计算省份ID
                        $city = M('area')->field('area_id,area_name')->where('area_parent_id=0')->select();
                        $province = array();
                        $tmp = explode(',', $areas[$key][0]);
                        if (!empty($tmp) && is_array($tmp)) {
                            foreach ($tmp as $t) {
                                $pid = $city[$t]['area_id'];
                                if (!in_array($pid, $province) && !empty($pid))
                                    $province[] = $pid;
                            }
                        }
                        if (count($province) > 0) {
                            $a = implode(',', $province);
                            $dataa[$key + 1]['top_area_id'] = ',' . $a . ',';
                        } else {
                            $dataa[$key + 1]['top_area_id'] = '';
                        }
                        M('transportExtend')->add($dataa[$key + 1]);
                    }
                }
                $this->success('运费模版添加成功！');
            } else {
                $this->error('运费模版修改失败！');
            }
        }
        $this->assign('action', 'transport');
        $this->display();
    }

    public function edit() {
        $trans_id = I('id');

        if (IS_POST) {
            $areass = explode('|||', I('areas')['kd'][1]);
            $city = M('area')->field('area_id,area_name')->where('area_parent_id=0')->select();
            $province = array();
            $tmp = explode(',', $areass[0]);

            if (!empty($tmp) && is_array($tmp)) {
                foreach ($tmp as $t) {
                    $pid = $city[$t]['area_id'];
                    if (!in_array($pid, $province) && !empty($pid))
                        $province[] = $pid;
                }
            }

            if (count($province) > 0) {
                $a = implode(',', $province);
                $data['top_area_id'] = ',' . $a . ',';
            } else {
                $data['top_area_id'] = '';
            }

            $data['title'] = I('title');
            $data['send_tpl_id'] = 1;
            $data['store_id'] = session('store_id');
            $data['num'] = I('num');
            $data['money'] = I('money');
            $where1['id'] = $trans_id;
            $rs = M('transport')->where($where1)->save($data);
            if ($rs !== FALSE) {
                //编辑成功后删除附加表
                $where2['transport_id'] = $trans_id;
                M('transportExtend')->where($where2)->delete();

                $default = I('default');
                $datas['snum'] = $default['kd']['start'];
                $datas['sprice'] = $default['kd']['postage'];
                $datas['xnum'] = $default['kd']['plus'];
                $datas['xprice'] = $default['kd']['postageplus'];
                $datas['is_default'] = 1;
                $datas['transport_id'] = $trans_id;
                $datas['transport_title'] = I('title');
                $datas['area_name'] = '全国';
                M('transportExtend')->add($datas);
                $areas = I('areas')['kd'];
                $special = I('special')['kd'];
                if (is_array($areas) && is_array($special)) {
                    foreach ($special as $key => $value) {
                        if (empty($areas[$key]))
                            continue;
                        $areas[$key] = explode('|||', $areas[$key]);
                        $dataa[$key + 1]['area_id'] = ',' . $areas[$key][0] . ',';
                        $dataa[$key + 1]['area_name'] = $areas[$key][1];
                        $dataa[$key + 1]['snum'] = $value['start'];
                        $dataa[$key + 1]['sprice'] = $value['postage'];
                        $dataa[$key + 1]['xnum'] = $value['plus'];
                        $dataa[$key + 1]['xprice'] = $value['postageplus'];
                        $dataa[$key + 1]['is_default'] = 2;
                        $dataa[$key + 1]['transport_id'] = $trans_id;
                        $dataa[$key + 1]['transport_title'] = I('title');

                        //计算省份ID
                        $city = M('area')->field('area_id,area_name')->where('area_parent_id=0')->select();
                        $province = array();
                        $tmp = explode(',', $areas[$key][0]);
                        if (!empty($tmp) && is_array($tmp)) {
                            foreach ($tmp as $t) {
                                $pid = $city[$t]['area_id'];
                                if (!in_array($pid, $province) && !empty($pid))
                                    $province[] = $pid;
                            }
                        }
                        if (count($province) > 0) {
                            $a = implode(',', $province);
                            $dataa[$key + 1]['top_area_id'] = ',' . $a . ',';
                        } else {
                            $dataa[$key + 1]['top_area_id'] = '';
                        }
                        M('transportExtend')->add($dataa[$key + 1]);
                    }
                }
                $this->success('运费模版修改成功！', U('index'));
            } else {
                $this->error('运费模版修改失败！');
            }
        }

        $map['id'] = $trans_id;
        $list = M('transport')->where($map)->find();
        $where['transport_id'] = $list['id'];
        $list['trans'] = M('transport_extend')->where($where)->select();

        $this->assign('list', $list);
        $this->assign('action', 'transport');
        $this->display();
    }

    //删除
    public function del() {
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        if (M('transport')->where('id=' . $Id)->delete()) {
            //编辑成功后删除附加表
            $where2['transport_id'] =$Id;
            M('transportExtend')->where($where2)->delete();
            $this->success('删除成功！', U('index'));
        } else {
            $this->error('删除失败！');
        }
    }

}
